package com.wwp.tbshop.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wwp.tbshop.R;
import com.wwp.tbshop.event.SaleNewsCartEventbus;
import com.wwp.tbshop.model.adapter.SaleNewsRVAdapter;
import com.wwp.tbshop.model.bean.SaleNewsBean;
import com.wwp.tbshop.presenter.SaleNewsPresenter;
import com.wwp.tbshop.util.utils.TimeTools;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by ${wenping} on 6/15/2017.
 */

public class SaleNewsActivity extends Activity {
    private SparseArray<CountDownTimer> countDownMap;

    @Bind(R.id.sale_news_recycleview)
    RecyclerView mSaleNewsRecycleview;
    @Bind(R.id.sale_news_back)
    ImageView mSaleNewsBack;
    @Bind(R.id.sale_news_cart)
    ImageView mSaleNewsCart;
    @Bind(R.id.sale_news_loading)
    public RelativeLayout mSaleNewsLoading;
    @Bind(R.id.sale_news_count_time)
    TextView mSaleNewsCountTime;
    @Bind(R.id.loading)
    public ProgressBar mLoading;

    private SaleNewsRVAdapter mSaleNewsRVAdapter;
    private static final String TAG = "SaleNewsActivity";
    private int mTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale_news);
        ButterKnife.bind(this);

       /* mLoadingView = View.inflate(UiUtil.getContext(), R.layout.pager_loading, null);
        mLoadingView.setVisibility(View.VISIBLE);*/
        mLoading.setVisibility(View.VISIBLE);
        SaleNewsPresenter saleNewsPresenter = new SaleNewsPresenter(this);
        saleNewsPresenter.loadSaleNewsInfo();
        mSaleNewsRecycleview.setLayoutManager(new GridLayoutManager(this, 2));
        mSaleNewsRVAdapter = new SaleNewsRVAdapter(this, this);
        mSaleNewsRecycleview.setAdapter(mSaleNewsRVAdapter);

        countDownTime();
    }





    // 2,60,60
    private void countDownTime() {
        countDownMap = new SparseArray<>();
        mTime = 7200;
        Log.e(TAG, "countDownTime "+ System.currentTimeMillis());
//        mTime = (int) (mTime- System.currentTimeMillis());
        if (mTime >0){
            CountDownTimer countDownTimer = new CountDownTimer(mTime*1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    mSaleNewsCountTime.setText(TimeTools.getCountTimeByLong(millisUntilFinished));
                }

                @Override
                public void onFinish() {
                    mSaleNewsCountTime.setText("00:00:00");
                }
            }.start();
            countDownMap.put(mSaleNewsCountTime.hashCode(),countDownTimer);
        }else {
            mSaleNewsCountTime.setText("00:00:00");
        }
    }

    public void onSaleNewsSuccess(List<SaleNewsBean.TopicBean> list) {
        mSaleNewsRVAdapter.setSalesNewsData(list);
        mLoading.setVisibility(View.GONE);

        Toast.makeText(this, "" + list.size(), Toast.LENGTH_LONG).show();
    }

    @OnClick({R.id.sale_news_back, R.id.sale_news_cart})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sale_news_back:
                finish();//结束本界面,返回
                break;
            case R.id.sale_news_cart:
                //跳转到购物车;有问题呢;
                SaleNewsCartEventbus eventbus = new SaleNewsCartEventbus();
                eventbus.SalenewsToMain(this);
                EventBus.getDefault().post(eventbus);
                break;
        }
    }
}
